package com.zeus.controller;

import com.github.pagehelper.PageInfo;
import com.zeus.pojo.Goods;
import com.zeus.pojo.GoodsSku;
import com.zeus.pojo.SolrPager;
import com.zeus.service.GoodsService;
import com.zeus.service.GoodsSkuService;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;
import java.util.List;

/**
 * @author 冯扬龙
 * @date 2021/4/17 16:30
 */
@Controller
@RequestMapping("/goods")
public class GoodController {

    @Autowired
    private HttpSolrClient httpSolrClient;
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private GoodsSkuService goodsSkuService;

//   点击不同的商品列表跳转到专门属于此商品的详细页面，点击按钮，传过来的参数是cid, 利用solr进行查询
    @RequestMapping("/showGoods")
    public ModelAndView showGoodsByWhereBySolr(@RequestParam(name = "pageIndex", defaultValue = "1") int pageIndex, @RequestParam(name = "cid", defaultValue = "0") int cid, @RequestParam(name = "keyword", defaultValue = "") String keyword) throws IOException, SolrServerException {
        //通过solr客户端 查询 数据
        //1.创建查询对象
        SolrQuery solrQuery = new SolrQuery();
        //2.设置默认查询的字段
        solrQuery.set("df", "goodsName");
        if (keyword == "")
            solrQuery.setQuery("*");
        else
            solrQuery.setQuery(keyword + "*");
        //3.设置过滤条件
        if (cid != 0)
            solrQuery.setFilterQueries("cid:" + cid);
        //4.设置组装的字段
        solrQuery.addField("id");
        solrQuery.addField("goodsName");
        solrQuery.addField("salesPrice");
        solrQuery.addField("goodsImg");
        //5.查询分页数据
        int pageSize = 2;
        //设置偏移量
        solrQuery.setStart((pageIndex - 1) * pageSize);
        //设置每页大小
        solrQuery.setRows(pageSize);
        //6.将查询对象传递到服务器端
        QueryResponse queryResponse = httpSolrClient.query(solrQuery);
        httpSolrClient.commit();
        //7.返回查询到的当前页数据
        List<Goods> goodsList = queryResponse.getBeans(Goods.class);
        //返回的是总记录数
        long recordCount = queryResponse.getResults().getNumFound();
        //8.组装分页类对象

        SolrPager solrPager = new SolrPager();
        solrPager.setPageIndex(pageIndex);
        solrPager.setPageSize(pageSize);
        solrPager.setGoodsList(goodsList);
        solrPager.setRecordCount(recordCount);
        //计算总页数
        int pageCount = (int) Math.ceil((double) recordCount / pageSize);
        solrPager.setPageCount(pageCount);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("solrPager", solrPager);
        modelAndView.addObject("cid", cid);
        modelAndView.addObject("keyword", keyword);
        modelAndView.setViewName("search2");
        return modelAndView;

    }

//    前端通过点击传一个goodsId过来，然后进数据库查询，把对象组装好以后返回一个goods类型的对象
    @RequestMapping("selectGoodsByGoodsId")
    public ModelAndView selectGoodsByGoodsId(int goodsId){
        Goods goods = goodsService.selectGoodsByGoodsId(goodsId);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("goods",goods);
//        introduction为商品展示页面
        modelAndView.setViewName("introduction");
        return modelAndView;
    }

//  商品不同尺码对应不同的颜色跟不同的样式，前端传入sku_propertiesd+goodsid
    @RequestMapping("/getSkuGoods")
    @ResponseBody
    public GoodsSku getSkuGoods(String sku_properties,int goodsId){
        GoodsSku skuGoods = goodsSkuService.getSkuGoods(sku_properties,goodsId);
        return skuGoods;
    }

}
